﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sio.Mdm.Client;
using Sio.Mdm.BF.Administracija;
using System.Diagnostics;
using Sio.Mdm.Entities;
using Sio.Mdm.Types.Database;
using Sio.Mdm.BF.Sifranti;
using Sio.Mdm.BF.Idm;
using Sio.Mdm.Types.Settings;
using Sio.Mdm.Entities.Linq.Factory;
using Sio.Mdm.BL.Base;

namespace Sio.Mdm.BLTest
{
    class Program
    {
        static void Main(string[] args)
        {
            TestNovihFiltrov();

            /*
            Sio.Mdm.Server.Server.Initialize();
            var auth = ServiceFactory.GetService<ISkupineKandidatiService>();
            EntityCollection<SkupinaKandidati> kandidati = new EntityCollection<SkupinaKandidati>();

            var query = new Filter<SkupinaKandidati>(11);


            auth.Select(ref kandidati, query.Skip(0), null);
            */ 
             
            
            //EntityCollection<SkupinaKandidati> kandidati = new EntityCollection<SkupinaKandidati>(
            //        new Filter<SkupinaKandidati>(11));
            //TestStoredProcedur();
            //TestVstavljanjaUcecega();

            return;
            TestSettingov();
            return;

            TestNovihFiltrov();
            TestVstavljanjaUcecega();

            /*
            //var auth = ServiceFactory.GetService<IAvtorizacijaService>();
            Sio.Mdm.Types.Session session = null;
            //var error = auth.Login("test", "test", out session);

            /*
            var usersService = ServiceFactory.GetService<IUporabniskiRacuniService>();
            EntityCollection<UporabniskiRacun> users = null;
            error = usersService.Select(ref users, r => r.IDM_STEVILKA != null, session);
             */
            

            //var error = slService.SelectWithJoin<UcecStatus, SolskoLeto>(ref uceci, (r, t, z) => (r.VlogaId == t.VlogaId) && (z.LetoId == t.LetoId) && (z.LetoId == 2011), null);
            
            Console.ReadKey(true);
        }

        public static void TestSettingov()
        {
            var slService = EntityFactory.CreateInstance<UserSettings>();
            slService.LinesPerPage = 10;

            var slService2 = EntityFactory.CreateInstance<UserSettings>();
            Console.WriteLine(slService2.LinesPerPage);
            Console.ReadLine();
        }

        public static void TestStoredProcedur()
        {
            var filter = new Filter<UporabniskiRacun>().Where(r => r.UporabniskoIme == "matej" && r.Geslo == "matej" && r.OrganizacijaId == 1);
            IEntityCollection col = new EntityCollection<UporabniskiRacun>(filter); 
        }

        public static void TestVstavljanjaUcecega()
        {
            Sio.Mdm.Types.Session session = null;
            Ucec ucec = EntityFactory.CreateInstance<Ucec>();
            ucec.Ime = "Novo ime";
            ucec.Priimek = "Nov priimek";
            var slService = ServiceFactory.GetService<IUceciService>();
            slService.Create(ucec, session);
            //ucec.UporabniskoIme = "Username";
            //slService.Update(ucec, session);
            slService.Delete(ucec, session);
            //ucec.Update();
            //var slService = ServiceFactory.GetService<IUceciService>();
            //slService.Create(ucec, session);
        }

        //public EntityCollection<T> MakeSelect<T>(Expression<Func<T, T1, bool>> filter) where T : class, IEntity
        public static EntityCollection<T> MakeSelect<T>(ISioFilter<T> filter) where T : class, IEntity
        {
            var exp = filter.Expression;
            var sql = Sio.Mdm.BL.Base.SqlOperations.BuildSelectCommand<T>(SelectType.Data, filter, typeof(T), false, null);
            //var result = Sio.Mdm.BL.Base.SioMdmFilterFormatter.Format(exp, false);
            //var result = IQToolkit.Data.Firebird.FirebirdFormatter.Format(exp);
            throw new NotImplementedException();
        }

        private static void TestNovihFiltrov()
        {

            
            //var servis = ServiceFactory.GetService<IUceciService>();
            //EntityCollection<Ucec> uceci = null;
            int uceci = 0;
            //var err = servis.Select(ref uceci, servis.Filter().Where(r => (r.UporabniskoIme != null) || r.Ime.StartsWith("A")), null);

            //var err = servis.Select(ref uceci, servis.Filter().Where(r => (r.UporabniskoIme != null) || r.Ime.StartsWith("A")).OrderBy(r => r.Ime), null);

            //var filter = new Filter<UporabniskiRacun>().Where(r => r.UporabniskoIme == "matej" && r.Geslo == "matej" && r.OrganizacijaId == 1);
            //var filter = new Filter<StarsIzRazreda>(1).Where(r => r.Geslo == null).Where(u => u.IdmStevilka != null);

            //EntityCollection<SkupinaKandidati> kandidati = new EntityCollection<SkupinaKandidati>(
            //var filter = new Filter<Ucec>().LeftJoin(new Filter<UcecStatus>(), r => r.VlogaId.ToString()+"X", s => s.VlogaId.ToString()+s.LetoId.ToString(), (r, s) => r);
            var a = ":3";
            var filter = new Filter<Pravica>().LeftJoin(new Filter<VlogaPravica>(), r => r.PravicaId.ToString() + a.ToString(), s => s.PravicaId.ToString() + ':' + s.TipVlogeId.ToString(), (r, s) => r, (j, k) => (k.PravicaId == null));
            
            /*
             * var filter = from u in new Filter<StarsIzRazreda>(1)
                         join r in new Filter<StarsIzRazreda>(2) on u.IdmStevilka equals r.IdmStevilka
                         where u.IdmStevilka == 1
                         where r.IdmStevilka == 2
                         select u;
             */

                /*
            var filter = from u in new Filter<Ucec>().SelectByField(r => r.UporabniskoIme, uporabniki.ToArray())
                          //join s in new Filter<UcecStatus>() on u.VlogaId equals s.VlogaId
                          //join l in new Filter<SolskoLeto>() on s.LetoId equals l.LetoId
                          //where s.LetoId == 2010
                          select u;
                 */

            //var err = servis.Count(ref uceci, filter.Skip(1).Take(10), null);
            //filter = filter.Skip(1).Take(10);
            //filter = filter.AddPropertyMatchCaseInsensitive("Priimek", "a");
            var resutl = MakeSelect<Pravica>(filter);



            //Console.WriteLine(uceci.Count());
            //Console.ReadLine();

            //var err = servis.Select(ref uceci, filter.Skip(1).Take(10), null);
            
            /*
            var filterSestavljen = (from p in new Filter<Ucec>()//servis.Filter()
                         where (p.UporabniskoIme != null) || p.Ime.StartsWith("A")
                         orderby p.Ime
                         orderby p.Priimek descending
                         select p).Take(10);
             */
            //var err = servis.Select(ref uceci, filter, null);

            /*
            var filterOriginalni = from u in new Filter<Ucec>()
                                   join o in new Filter<Organizacija>() on u.OrganizacijaId equals o.OrganizacijaId
                                   where (u.UporabniskoIme != null) || u.Ime.StartsWith("A")
                                   where (o.OrganizacijaId == 1)
                                   select u;
             EntityCollection<Ucec> uceci = null;
var err = slService.SelectWithJoin<UcecStatus, SolskoLeto>(ref uceci, 
   (r, t, z) => (r.VlogaId == t.VlogaId) && (z.LetoId == t.LetoId) && (z.LetoId == 2011),
    session);
             * * 
             * 
             */
            //var filterOriginalni = new Filter<Ucec>().Where(r => r.OrganizacijaId == 1);
            //var resutl = MakeSelect<Ucec>(filter);

            /*
            var filterNov = filterOriginalni.Skip(10).Take(10);
            filterNov = filterNov.AddOrderBy("Ime").AddOrderByDescending("Priimek");

            filterNov = filterNov.AddPropertyMatch("Ime", "A%");
            */   
            //    new Filter<Ucec>().Where //.Where(p => (p.UporabniskoIme != null) || p.Ime.StartsWith("A")).Take(10).Skip(10).OrderBy(r => r.DavcnaStevilka).OrderByDescending(r => r.Emso).OrderBy(r => r.Drzavljanstvo);
            //var filter2 = FilterFactory.AddOrderBy(filterSestavljen, "Priimek");
            //var filter2 = FilterFactory.AddPropertyMatch(filterSestavljen, "Ime", "a");

            /*
            var slService = ServiceFactory.GetService<IUceciService>();
            EntityCollection<Ucec> uceci = null;
            var err = slService.Select(ref uceci, slService.Filter().OrderBy(r => r.DavcnaStevilka), null);


            var filterSestavljen = from p in slService.Filter()//new Filter<Ucec>()
                          where p.UporabniskoIme == null
                          select p;

            var err2 = slService.Select(ref uceci, filterSestavljen, null);
             */
        }
    }
}
